package com.huchao.www.controller;

import com.github.pagehelper.PageInfo;
import com.huchao.www.exception.MyException;
import com.huchao.www.pojo.StudentData;
import com.huchao.www.service.impl.StudentServiceImpl;
import com.huchao.www.pojo.StudentPo;
import com.huchao.www.utils.ExcelUtil;
import com.huchao.www.vo.UserSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Description:
 * @BelongsProject: background-management-v10
 * @BelongsPackage: com.huchao.www.controller
 * @Author: 亖思
 * @CreateTime: 2023/3/10 21:05
 * @Version: 1.0
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentServiceImpl studentService;

    @GetMapping("/student")
    public String stuManage() {
        return "html/student";
    }

    @RequestMapping("/list")
    public String adminList(UserSearch userSearch,
                            Model model,
                            @RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "10") int size
    ) {
        HashMap<String, Object> map = userSearch.getHashMap();
        List<StudentPo> stuPageList = studentService.findStuPageList(map, page, size);
        PageInfo<StudentPo> stuPageInfo = new PageInfo<>(stuPageList);
        model.addAttribute("pageInfo", stuPageInfo);
        return "html/student";
    }

    @RequestMapping("/DoImportExcel")
    public void DoImportExcel(@RequestParam(value = "excelFile") MultipartFile file) throws MyException, IOException {
        //判断file是否为Excel||前端accept属性限制
        if (!file.isEmpty()) {
            //对文文件的全名进行截取然后在后缀名进行删选。
            int begin = file.getOriginalFilename().indexOf(".");
            int last = file.getOriginalFilename().length();
            //获得文件后缀名
            String a = file.getOriginalFilename().substring(begin, last);
            //进行判断
            if (!a.endsWith(".xlsx") && !a.endsWith(".xls")) {
                throw new MyException("所选文件格式错误");
            }
        }
        if(studentService.importData(file).equals("success")){
            throw new MyException("导入成功");
        }
    }

    @RequestMapping("/downloadModel")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "导入用户模板";
        String sheetName = "导入用户模板";
        List<StudentData> stuList = new ArrayList<>();
        stuList.add(new StudentData(123456789, "张三", "653200200209287609", 1, "机电学院", "机械电子工程","20机械电子工程3班","2022-17-02  16:59:20"));
        try {
            ExcelUtil.writeExcel(response, stuList, fileName, sheetName, StudentData.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/export")
    public void exportData(HttpServletResponse response) {
        String fileName = "学生列表";
        String sheetName = "学生列表";
        List<StudentData> stuList = studentService.findAllStu();
        try {
            ExcelUtil.writeExcel(response, stuList, fileName, sheetName, StudentData.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
